/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.group;

import ohos.aafwk.ability.ILifecycle;
import ohos.aafwk.ability.ILifecycleObserver;
import ohos.aafwk.ability.Lifecycle;
import ohos.aafwk.ability.LifecycleObserver;
import ohos.aafwk.content.Intent;

/**
 * Created by JiangQi on 8/23/18.
 */
public abstract class UserVisibleHintGroupScene extends GroupScene {
    public static final String KEY_SCENE_USER_VISIBLE_HINT = "bd-scene-nav:scene_user_visible_hint";

    private final UserVisibleLifecycleOwner mUserVisibleLifecycleOwner = new UserVisibleLifecycleOwner();

    private boolean mUserVisibleHint = true;
    private boolean mResume = false;
    private boolean mStart = false;

    @Override
    public void onSaveInstanceState(Intent outState) {
        super.onSaveInstanceState(outState);
        outState.setParam(KEY_SCENE_USER_VISIBLE_HINT, this.mUserVisibleHint);
    }

    @Override
    public void onCreate(Intent savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) {
            this.mUserVisibleHint = savedInstanceState.getBooleanParam(KEY_SCENE_USER_VISIBLE_HINT, false);
        }
    }

    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (this.mUserVisibleHint == isVisibleToUser) {
            return;
        }
        mUserVisibleHint = isVisibleToUser;
        dispatchVisibleChanged();

        if (mUserVisibleHint) {
            if (mStart) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
            }
            if (mResume) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_ACTIVE);
            }
        } else {
            if (mResume) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_ACTIVE);
            }

            if (mStart) {
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_BACKGROUND);
            }
        }
    }

    @Override
    public void onAbilityCreated(Intent savedInstanceState) {
        super.onAbilityCreated(savedInstanceState);
        mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);

        getLifecycle().addObserver(new LifecycleObserver() {
            @Override
            public void onStart(Intent intent) {
                super.onStart(intent);
                mStart = true;
                if (mUserVisibleHint) {
                    mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_START);
                }
            }

            @Override
            public void onInactive() {
                super.onInactive();
                mResume = false;
                if (mUserVisibleHint) {
                    mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_BACKGROUND);
                }
            }

            @Override
            public void onActive() {
                super.onActive();
                mResume = true;
                if (mUserVisibleHint) {
                    mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_ACTIVE);
                }
            }

            @Override
            public void onBackground() {
                super.onBackground();
                mStart = false;
                if (mUserVisibleHint) {
                    mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
                }
            }

            @Override
            public void onStop() {
                super.onStop();
                mUserVisibleLifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
            }
        });
    }

    public boolean getUserVisibleHint() {
        return mUserVisibleHint;
    }

    public Lifecycle getUserVisibleHintLifecycle() {
        return this.mUserVisibleLifecycleOwner.getLifecycle();
    }

    public ILifecycle getUserVisibleHintLifecycleOwner() {
        return this.mUserVisibleLifecycleOwner;
    }

    @Override
    public boolean isVisible() {
        return super.isVisible() && mUserVisibleHint;
    }
}
